//
//  FlagToggleViewController.swift
//  333333333
//
//  Created by wangyong的MAC on 2021/9/7.
//
import UIKit
import Foundation
class FlagToggleViewController: UIViewController {
    private let flag: Flag<Bool>
    private lazy var label = UILabel()
    private lazy var toggle = UISwitch()
    init(flag: Flag<Bool>) {
        self.flag = flag
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        label.text = flag.name
        label.frame = CGRect.init(x: 100, y: 100, width: 150, height: 150)
        toggle.frame = CGRect.init(x: 100, y: 200, width: 150, height: 150)
        toggle.isOn = flag.wrappedValue
        self.view.addSubview(label)
        self.view.addSubview(toggle)
        toggle.addTarget(self,
            action: #selector(toggleFlag),
            for: .valueChanged
        )
    }
    @objc private func toggleFlag() {
        flag.wrappedValue = toggle.isOn
    }
}
